package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.ProductGuideMapper;
import com.yunhai.device.service.ProductGuideService;
import com.yunhai.model.entity.dms.DmsProductGuideEntity;
import com.yunhai.model.vo.dms.DmsProductGuideQuery;
import com.yunhai.model.vo.dms.DmsProductGuideVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductGuideServiceImpl implements ProductGuideService {

    @Resource
    private ProductGuideMapper productGuideMapper;

    /**
     * 获取 ProductGuide 分页列表
     */
    @Transactional
    public IPage getProductGuidePagination(PaginationVo paginationVo, DmsProductGuideQuery dmsProductGuideQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsProductGuideEntity> lqs = new LambdaQueryWrapper();
        productGuideMapper.selectPage(page, lqs);

        List<DmsProductGuideEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 ProductGuide 所有数据
     *
     */
    @Transactional
    public List<DmsProductGuideEntity> getProductGuideAllList( DmsProductGuideQuery dmsProductGuideQuery ) throws Exception {

        LambdaQueryWrapper<DmsProductGuideEntity> lqs = new LambdaQueryWrapper();
        List<DmsProductGuideEntity> list = productGuideMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 ProductGuide 详情
     */
    @Transactional
    public DmsProductGuideEntity getProductGuideDetail( Long id) throws Exception {

        DmsProductGuideEntity dmsProductGuideEntity = productGuideMapper.selectById(id);
        return dmsProductGuideEntity;
    }


    /**
     * 新增 ProductGuide
     * */
    @Transactional
    public void addProductGuide(DmsProductGuideVo dmsProductGuideVo) throws Exception {

        DmsProductGuideEntity dmsProductGuideEntity = new DmsProductGuideEntity();
        ReflectionCopy.copyProperties( dmsProductGuideEntity, dmsProductGuideVo );
        productGuideMapper.insert(dmsProductGuideEntity);
        return;
    }


    /**
     * 更新 ProductGuide
     * */
    @Transactional
    public void updateProductGuide( DmsProductGuideVo dmsProductGuideVo) throws Exception {

        if( dmsProductGuideVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsProductGuideEntity dmsProductGuideEntity = new DmsProductGuideEntity();
        ReflectionCopy.copyProperties( dmsProductGuideEntity, dmsProductGuideVo );
        productGuideMapper.updateById(dmsProductGuideEntity);
        return;
    }

    /**
     * 删除 ProductGuide
     * */
    @Transactional
    public void batchRemoveProductGuide( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        productGuideMapper.deleteBatchIds(ids);
    }

}